/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.widget;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.ActionWidget;

/**
 *
 * @author Marnix Renne
 */
public class Camera extends ActionWidget {
    public static final String CAMERA_WIDGET_TAG = "camera";
    private Canvas canvas;
    private Player player;
    private VideoControl videoControl;
    
    public Camera() {
        super(CAMERA_WIDGET_TAG);
        setId("camera");
        try {
            player = createPlayer();
            player.realize();
            videoControl = (VideoControl) player.getControl("VideoControl");
            videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, Kuix.getCanvas());
            videoControl.setVisible(true);
            //AdvancedMultimediaManager.setZoom(player);
            //AdvancedMultimediaManager.setExposure(player);
            player.start();
        } catch (IOException ex) {
            Kuix.alert(ex);
        } catch (MediaException ex) {
            Kuix.alert(ex);
        }
    }

    public void cleanUp() {
        super.cleanUp();
        if (player != null) {
            videoControl = null;
            try {
                player.stop();
            } catch (MediaException me) {
            // continue
            }
            player.deallocate();
            player.close();
            player = null;
        }
    }

    private Player createPlayer() throws IOException, MediaException {
        // Try a workaround for Nokias, which want to use capture://image in some cases
        Player player = null;
        String platform = System.getProperty("microedition.platform");
        if (platform != null && platform.indexOf("Nokia") >= 0) {
            try {
                player = Manager.createPlayer("capture://image");
            } catch (MediaException me) {
            //    Kuix.alert(me);
            // if this fails, just continue with capture://video
            } catch (Error e) {
            //    Kuix.alert(e);
            // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
            // We should still try to continue
            }
        }
        if (player == null) {
            player = Manager.createPlayer("capture://video");
        }
        return player;
    }

    /*
    public void paintImpl(Graphics g) {
        super.paintImpl(g);
        try {
            if (videoControl == null) {
                videoControl = (VideoControl) player.getControl("VideoControl");
                canvas = Kuix.getCanvas();
                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
                int x = getDisplayX();
                int y = getDisplayY();
                int width = getWidth();
                int height = getHeight();
                videoControl.setDisplayLocation(x, y);
                videoControl.setDisplaySize(width, height);
                videoControl.setVisible(true);
                player.start();
            }
        } catch (MediaException ex) {
            Kuix.alert(ex);
        }
    }
    */
    public Metrics getPreferredSize(int w) {
        Metrics metrics = new Metrics(this);
        metrics.width = videoControl.getDisplayWidth();
        metrics.height = videoControl.getDisplayHeight();
        return metrics;
    }
    protected void doLayout() {
        super.doLayout();
        videoControl.setDisplayLocation(getDisplayX(), getDisplayY());
        try {
            videoControl.setDisplaySize(getWidth(), getHeight());
        } catch (MediaException e) {
            Kuix.alert(e);
        }
    }
    public byte[] takeSnapshot() {
        byte[] snapshot = null;
        try {
            snapshot = videoControl.getSnapshot(null);
        } catch (MediaException me) {
        }
        if (snapshot == null) {
            try {
                // Fall back on JPEG; seems that some cameras default to PNG even
                // when PNG isn't supported!
                snapshot = videoControl.getSnapshot("encoding=jpeg");
                if (snapshot == null) {
                    throw new MediaException("Can't obtain a snapshot");
                }
            } catch (MediaException ex) {
                Kuix.alert(ex);
            }
        }
        return snapshot;
        
    }
}
